#include<input_manager.h>

static struct tsdev *g_ts;

/* tslib.h
struct ts_sample {
	int		x;
	int		y;
	unsigned int	pressure;
	struct timeval	tv;
};
*/

static int tsGetInputEvent (PInputEvent ptInputEvent)
{
	int ret;
	struct ts_sample samp;
	
	ret = ts_read(g_ts, &samp, 1);
	if (ret < 0) 
	{
		printf("ts_read err\n");
		ts_close(g_ts);
		return -1;
	}
	
	ptInputEvent->iType     = INPUT_TYPE_TOUCH;
	ptInputEvent->iX        = samp.x;
	ptInputEvent->iY        = samp.y;
	ptInputEvent->iPressure = samp.pressure;
	ptInputEvent->tTime     = samp.tv;

	return 0;
}

static int tsDeviceInit(void)
{
	g_ts = ts_setup(NULL, 0);    //打开并且配置输入设备
	if (!g_ts)
	{
		printf("ts_setup err\n");
		return -1;
	}
	return 0;
}

static int tsDeviceExit(void)
{
	ts_close(g_ts);
	return 0;
}

static InputDevOpr g_tTouchScreenOpr = {
	.name          = "ts",
	.GetInputEvent = tsGetInputEvent,
	.DeviceInit    = tsDeviceInit,
	.DeviceExit    = tsDeviceExit,
};
 

void TouchScreenRegister(void)
{
	RegisterInputDev(&g_tTouchScreenOpr);
}

